<?php
error_reporting( E_ALL | E_STRICT );
include('functions.php');

// Define the different number of documents to read in each run
$runs = array(
	'10',
	'100',
	'1000',
	'10000',
	'20000',
	'30000',
	'40000',
	'50000',
	'60000',
	'70000',
	'80000',
	'90000',
	'100000',
	'200000',
	'300000',
	'400000',
	'500000'
);

// Call the function 'pickRandDoc()' to pick a random document ID.
// This is because MongoDB does not provide a "get random document" function
$rand = pickRandDoc();
$count = count($rand) - 1;

l("findOne");
foreach($runs as $r) {
	set_time_limit(0);
	ini_set("memory_limit","500M");
	$start = timeit();
	for($i=0; $i<$r; $i++) {
		// Choose a random document ID
		$ran = $rand[mt_rand(0,$count)];
		$query = array('_id' => $ran);
		
		// Execute query with 'findOne'
		$doc = q('post')->findOne($query);
	}
	$end = timeit();
	$time = number_format($end - $start, 15);
	l($time);
	sleep(2);
}

l("\nfind + limit(1)");
foreach($runs as $r) {
	set_time_limit(0);
	ini_set("memory_limit","500M");
	$start = timeit();
	for($i=0; $i<$r; $i++) {
		$ran = $rand[mt_rand(0,$count)];
		$query = array('_id' => $ran);
		
		// Execute query with 'find' and 'limit(1)'
		$doc = q('post')->find($query)->limit(1);
	}
	$end = timeit();
	$time = number_format($end - $start, 15);
	l($time);
	sleep(2);
}

echo "<h1>Done</h1>";
?>